﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class DAO_NguoiNhan
    {
        public string GetTenNN(int maNN)
        {
            string querySearch = "SELECT HoTen FROM NGUOINHAN where MaNguoiNhan = " + maNN.ToString();
            DataTable dt = DataProvider.ExecuteQuery(querySearch);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                DataRow dr = dt.Rows[0];
                return dr[0].ToString();
            }
        }
        public int Insert(DTO_NguoiNhan receiver)
        {
            string query = "INSERT INTO NGUOINHAN VALUES (N'" + receiver.HoTen + "', '" + receiver.Email + "', '" + receiver.DienThoai + "', N'" + receiver.TP +" "+receiver.DiaChi+"')";
            DataProvider.ExecuteQuery(query);
            List<DTO_NguoiNhan> temp = new List<DTO_NguoiNhan>();
            temp = SelectAllMa();
            int max = 0;
            for (int i = 0; i < temp.Count; i++)
                if (temp[i].MaNguoiNhan > max)
                    max = temp[i].MaNguoiNhan;
            return max;
        }

        public List<DTO_NguoiNhan> SelectAllMa()
        {
            List<DTO_NguoiNhan> donhang = new List<DTO_NguoiNhan>();
            string querySelectAll = "SELECT [MaNguoiNhan] FROM NGUOINHAN";
            DataTable dt = DataProvider.ExecuteQuery(querySelectAll);
            foreach (DataRow dr in dt.Rows)
            {
                DTO_NguoiNhan dto_donhang = new DTO_NguoiNhan();
                dto_donhang.MaNguoiNhan = int.Parse(dr[0].ToString());
                donhang.Add(dto_donhang);
            }
            return donhang;
        }

        public void Delete(int ma)
        {
            string query = "DELETE FROM NGUOINHAN WHERE [MaNguoiNhan]=" + ma.ToString();
            DataProvider.ExecuteQuery(query);
        }
    }
}
